<template>
  <cj-popup v-model="isShowSelectBill" round position="bottom" :closeable="true" title="请选择要购买的票的类型" @close="closeBillDialog">
    <!-- <open-modal :openModal='isShowSelectBill' @closeModal='closeBillDialog'> -->
    <article class="c-bg-sgray c-p">
      <!-- <i @click="closeBillDialog" class="iconfont icon-cha c-pa c-p-r0 c-p-t0 c-ph24 c-pv24 c-fc-gray c-fs32"></i>
      <section class="c-pt40 c-pb30 c-fs28 c-fw-b c-textAlign-c">请选择要购买的票的类型</section> -->
      <div class="c-ph24 c-fs22 c-fc-gray c-pv20">您的身份是{{vipTypeMap[vipType]}}</div>
      <section class="c-maxh660 c-contexty-auto">
        <activity-bill-list
          v-if="ticketData.length"
          :ticketData="ticketData"
          :selectMtkType="selectMtkType"
          :selectId="selectId"
          :groupId="groupId"
          :vipType="vipType"
          :select="select"
          :recType="recType"
          :isSummitActivity="isSummitActivity"
          @slectTicket="slectTicket"
        ></activity-bill-list>
      </section>
      <footer class="c-w100 c-hh100 c-flex-row c-flex-center c-bd-t1">
        <div @click="toBuyActivity()" class="c-ww592 c-hh64 c-br32 c-flex-row c-flex-center c-fc-white c-fs28 theme-bg">确定</div>
      </footer>
      <!-- 选择阶梯 -->
      <SelectStairCollage v-if="isShowStair" :isShowStair.sync="isShowStair" :collageObject="collageObject" @clickNext="toBuyActivity"/>
      <!-- 关注二维码 -->
      <OfficalAccount :showAccountQcode="showAccountQcode" :desc="'关注我们后才可发起砍价哦'" :imgSrc="$addXossFilter(mpQrcode)" @hideDialog="hideDialog"
      />
    </article>
  <!-- </open-modal> -->
  </cj-popup>
</template>
<script>
import ActivityBillList from '@/components/activity/activityBillList';
import SelectStairCollage from "@/components/templates/common/selectStairCollage.vue";
import { utilJs } from "@/utils/common.js"
import OfficalAccount from '@/components/templates/common/officalAccount';
export default {
  name: "SelectActivityBill",
  components: {
    ActivityBillList,
    SelectStairCollage,
    OfficalAccount,
  },
  props: {
    isSummitActivity: { // isSummitActivity 特定活动(5000人峰会),
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      isShowSelectBill:false,
      select: -1, // 选择的票de index
      recType: 0, //推荐类型 1 访问 2 绑定 3 支付
      selectId:'',
      selectMtkType:'',
      ticketId: null, // 选择的票的id
      isFree: 0, // 是否免费 0 免费 1 收费
      vipType: 1, // 用户身份
      vipTypeMap: {},
      ticketData: [],
      companyAuth: JSON.parse(localStorage.getItem("companyAuth")),
      isShowStair: false,
      collageObject: null,
      showAccountQcode: false,
      mpQrcode: '',
    }
  },
  filters: {
    formatStringTime(value) {
      if (value != undefined && value != "" && value != null) {
        return value.substr(0, 10).replace(/-/g, '.');
      } else {
        return value;
      }
    },
  },
  created() {
    utilJs.appShare(this);
  },
  deactivated() {
    this.isShowSelectBill = false;
  },
  methods: {
    public_openSelectBills(info) {
      this.companyAuth = JSON.parse(localStorage.getItem("companyAuth"));
      this.vipTypeMap = utilJs.getCustNameMap();
      this._goFormQuery = info.formQuery || null;
      this._goodsRoomId = info.goodsRoomId;
      this._refereeId = info.refereeId;
      this._recRelateId = info.recRelateId;
      this._activityId = info.activityId;
      this._collageId = info.collageId || -1;
      this._groupId = info.groupId || -1;
      this.selectId = info.prodId || '';
      this.selectMtkType = info.mtkType || '';
      this.recType = info.recType || '';
      this.isShowSelectBill = true;
      this.getActivityBills();
    },
    closeBillDialog() {
      this.isShowSelectBill = false;
      this.showAccountQcode = false;
      this.isShowStair = false;
    },
    hideDialog() {
      this.showAccountQcode = false;
    },
    // 判断是否关注公众号
    checkUserIsFollowedMp(bargainId) {
      return new Promise(function (resolve, reject) {
        utilJs.getMethod(`${global.apiurl}bargains/getMpData?bargainId=${bargainId}`,
          res => {
            resolve(res);
          },
          failRes => {
            reject(failRes)
          }
        )
      })
    },
    toBuyActivity(index = -1) {
      // 点击确定
      if (this.select == -1) {
        return;
      }
      let selectItem = this.ticketData[this.select];
      let goPathQuery = {
        activityId: this._activityId,
        ticketId: this.ticketId,
        recType: this.recType,
        recRelateId: this._recRelateId
      };
      if (selectItem && selectItem.mtkType) {
        goPathQuery.mtkType = selectItem.mtkType; // 同个票券可能会参加营销活动，所以需要把营销类型也传过去判断是哪个票
      }
      if (this._goodsRoomId) {
        goPathQuery.goodsRoomId = this._goodsRoomId;
      }
      if (this._refereeId) {
        goPathQuery.refereeId = this._refereeId;
      }
      // optimalPriceType：1 产品本身价格 2 vip价格 3 svip价格 4 服务商 101 限时购 102 拼团 112 砍价  10 链动会员 20 链动代理商 99 链动复购
      //判断是否是阶梯拼团 index < 0 说明不是阶梯拼团组件SelectStairCollage的回调
      if (this.isFree == 1 && (!selectItem.optimal || (selectItem.optimal && selectItem.optimal.optimalPriceType == 102)) && (selectItem.mtkType == 2 || this.selectMtkType == 2) && selectItem.collage && selectItem.collage.status == 2 && selectItem.collage.collageId > 0) {
        if (index < 0 && selectItem.collage.collageGroupType == 2 && this._groupId == -1) {
          this.collageObject = selectItem;
          this.isShowStair = true;
          return;
        }
        goPathQuery.groupId = -1;
        if (this._groupId != -1 && this.selectId == selectItem.atId && this.selectMtkType == selectItem.mtkType) {
          goPathQuery.groupId = this._groupId;
        }
        goPathQuery.collageId = selectItem.collage.collageId;
        goPathQuery.selectId = this.selectId;
        goPathQuery.collagePriceIndex = index
      }
      //砍价票
      if ((!selectItem.optimal || (selectItem.optimal && selectItem.optimalPriceType == 112)) && (selectItem.mtkType == 12 || this.selectMtkType == 12) && selectItem.bargain != null && (selectItem.bargain.status == 2 || (selectItem.bargain.isPreheat == 1 && selectItem.bargain.status == 1))) {
        if (selectItem.bargain.userType == 1) {
          this.$showCjToast({
            text: '您当前有砍价活动正在进行中，由于该活动不可重复下单，现无法再次发起!',
            type: 'warn'
          });
          return;
        }
        this.checkUserIsFollowedMp(selectItem.bargain.bargainId).then(res => {
          if (res.threshold == 2 && res.isFollowedMp == 0) {
            this.mpQrcode = res.mpQrcode;
            this.showAccountQcode = true;
            return;
          }
        })
      }
      //如果有关联表单，先跳表单页填写完再到填票页
      let path = '/activity/activityEnroll';
      let query = goPathQuery;
      if (this._goFormQuery) {
        path = '/homePage/form';
        this._goFormQuery.goBillQuery = encodeURIComponent(JSON.stringify(goPathQuery));
        query = this._goFormQuery;
      }
      this.$routerGo(this, "push", {
        path: path,
        query: query
      })
      this.closeBillDialog();
    },
    getIsFree() {
      let item = this.ticketData[this.select];
      //免费票
      if (item.permission == 19) {
        return 0
      }
      //vip免费
      if (this.vipType == 2 && item.vipDiscount == 0) {
        return 0
      }
      //svip免费
      if (this.vipType == 4 && item.svipDiscount == 0) {
        return 0
      }
       //服务商免费
      if ((this.vipType == 3 || this.vipType >= 5) && item.commonProdInfo.agentDiscount == 0) {
        return 0
      }
      return 1
    },
    slectTicket(index) {
      this.select = index;
      this.ticketId = this.ticketData[index].atId;
      this.isFree = this.getIsFree();
    },
    getActivityBills() {
      let data = {
        activityId:this._activityId,
        collageId:this._collageId,
        groupId:this._groupId,
      }
      utilJs.postMethod(`${global.apiurl}activities/mgticket`, data, res => {
        this.vipType = res.retUserType;
        this.ticketData = res.data;
        if (!this.ticketData.length) {
          return;
        }
        // 寻找目标对象
        let selectIndex = this.ticketData.findIndex((item) => {
          return item.atId == this.selectId && item.mtkType == this.selectMtkType && item.number1 > 0 && item.number1 >= item.least;
        })
        if (selectIndex == -1) {
          // 当未寻找到符合得目标对象时
          selectIndex = this.ticketData.findIndex((value) => {
            return value.number1 > 0 && value.number1 >= value.least
          })
        }
        if (selectIndex != -1) {
          this.select = selectIndex;
          this.ticketId = this.ticketData[selectIndex].atId;
          this.isFree = this.getIsFree();
        }
      }, () => {
        this.isShowSelectBill = false;
      })
    },
  },
}
</script>
<style scoped>
.c-bg-ticket {
  width: 100%;
  height: 100%;
}
.c-zindex1 {
  z-index: 1;
}
.certain-button {
  position: fixed;
  bottom: 0;
  z-index: 11;
  max-width: 640px;
  width: 100%;
  display: flex;
  display: -webkit-flex;
  align-items: center;
  -webkit-align-items: center;
}
.c-pb260 {
  padding-bottom: 6.5rem;
}
.vux-popup-dialog {
  background-color: #ffffff !important;
}
</style>
